package Lono::Action::LoadFromWeb;

use strict;
use warnings;

use Apache::Constants qw( :common );
use Apache::Request;
use Template;

use Lono::Fetcher;

sub handler {
    my $r = Apache::Request->new( shift );
    
    $r->send_http_header( "text/html" );

    if ( $r->param( "url" ) ) {
        my $fetcher = Lono::Fetcher->new;
        my $response = $fetcher->from_url( $r->param( "url" ) );

        my @LOGS = $response->log->get_messages;
        foreach my $item ( @LOGS ) {
            warn $item->{'severity'} . ": " . $item->{'message'};   
        }

        if ( $response->is_success ) {
            my $recipe = $response->get_recipe;
        } else {
            $r->print( "no recipe for you!" );
        }

        my $template = Template->new({ 
        	    INCLUDE_PATH  => $r->dir_config( "TT_DIR" ),
    	        OUTPUT        => $r,     # direct output to Apache request
        });

        $template->process("importer/entry.tmpl", {'response' => $response});
    }
    
    return OK;
}

1;
